package com.lenovo.smarttraffic.ui.activity;

import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;

import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.MarkerOptions;

import com.amap.api.maps.model.PolylineOptions;
import com.lenovo.smarttraffic.R;

import java.util.ArrayList;


/**
 * 离线地图界面
 * @author Administrator
 *
 */

public class OfflineMapActivity extends BaseActivity {
    private MapView myMapView;
    private AMap aMap;
    private Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        myMapView.onCreate(savedInstanceState);
        if (aMap==null) {
            aMap = myMapView.getMap();
        }
}


    private void initView() {
        myMapView = (MapView) findViewById(R.id.my_map_view);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(v->{
            //封装经纬度,纬度在前，精度在后
            LatLng latLng = new LatLng(31.5039, 119.5818);
            ArrayList<LatLng> latLngs = new ArrayList<>();

            //如何操作一组点
            LatLngBounds.Builder builder = LatLngBounds.builder();
            for (int i = 0; i < 6; i++) {
                LatLng latLng1 = new LatLng(31.5039 + 0.3 * i, 119.5818 + 0.3 * i);
                latLngs.add(latLng1);
                aMap.addMarker(new MarkerOptions().position(latLng1).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                        .decodeResource(getResources(), R.mipmap.marker))));
                builder.include(latLng1);
            }
            //标点
            aMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                    .decodeResource(getResources(), R.mipmap.map_location))));
            LatLngBounds build = builder.build();
            aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(build, 300));
            aMap.addPolyline(new PolylineOptions().addAll(latLngs).
                    color(Color.RED).width(15f));
            float v1 = AMapUtils.calculateLineDistance(latLngs.get(0), latLngs.get(1));
            Toast.makeText(this, "两点之间的距离为"+v1, Toast.LENGTH_SHORT).show();
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        myMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        myMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myMapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        myMapView.onSaveInstanceState(outState);
    }



    @Override
    protected int getLayout() {
        return R.layout.activity_offline_map;
    }



}